package net.pulseproject.commons.midi.collection;

import java.util.Collection;

import net.pulseproject.commons.midi.entity.Device;
import net.pulseproject.commons.misc.collection.Filter;
import net.pulseproject.commons.misc.collection.Matcher;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DeviceFilter extends Filter<Device> {

	private static final Log LOG = LogFactory.getLog(DeviceFilter.class);
	
	private final TransmittingDeviceMatcher transmittingDeviceMatcher = new TransmittingDeviceMatcher();
	
	private final ReceivingDeviceMatcher receivingDeviceMatcher = new ReceivingDeviceMatcher();
	
	
	public final Collection<Device> filterTransmitting(final Collection<Device> allDevices) {
		LOG.debug("filterTransmitting(allDevices)");
		return this.filter(allDevices, this.transmittingDeviceMatcher);
	}

	public final Collection<Device> filterReceiving(final Collection<Device> allDevices) {
		LOG.debug("filterReceiving(allDevices)");
		return this.filter(allDevices, this.receivingDeviceMatcher);
	}
	
	public final InOutDevices filterInOutDevices(final Collection<Device> allDevices) {
		LOG.debug("filterInOutDevices(allDevices)");
		final Collection<Device> transmitters = this.filterTransmitting(allDevices);
        final Collection<Device> receivers = this.filterReceiving(allDevices);
        return new InOutDevices(transmitters, receivers);
	}

	private static class TransmittingDeviceMatcher implements Matcher<Device> {
		TransmittingDeviceMatcher() { /* nothing to do, just to avoid synthetic access */ }
		@Override
		public boolean matches(final Device instance) {
			return instance.getState().isTransmitting();
		}
	}

	private static class ReceivingDeviceMatcher implements Matcher<Device> {
		ReceivingDeviceMatcher() { /* nothing to do, just to avoid synthetic access */ }
		@Override
		public boolean matches(final Device instance) {
			return instance.getState().isReceiving();
		}
	}
}
